package com.blockmeta.bbs.baselibrary.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.blockmeta.bbs.baselibrary.R;
import com.blockmeta.bbs.baselibrary.base.app.BaseApp;


/**
 * Toast 工具类.简化Toast的使用，并且不用关心线程的问题。
 * Toast的初始化会创建默认构造Handler。Handler默认构造会使用当前线程Looper。如果没有，抛异常。
 * 调用了Looper.prepare和Looper.loop以后Toast才可以生效。但是子线程调用了loop方法就阻塞了。
 * 所以选择抛到主线程执行。
 */
public class ToastUtil {
    private static Handler sHandler;

    /**
     * show a short toast
     *
     * @param text
     */
    public static void show(Context context,String text) {
        safeShow(context,text, Toast.LENGTH_SHORT);
    }

    /**
     * show a short toast
     *
     * @param text
     */
    public static void show(String text) {
        safeShow(BaseApp.getApp(),text, Toast.LENGTH_SHORT);
    }

    /**
     * show a long toast
     *
     * @param text    string text
     */
    public static void showLong(Context context,String text) {
        safeShow(context,text, Toast.LENGTH_LONG);
    }

    /**
     * show a long toast
     *
     * @param text    string text
     */
    public static void showLong(String text) {
        safeShow(BaseApp.getApp(),text,Toast.LENGTH_LONG);
    }

    /**
     * show a short toast
     *
     * @param resId   string id
     */
    public static void show(int resId) {
        show(BaseApp.getApp(),BaseApp.getApp().getString(resId));
    }


    /**
     * 安全弹出Toast。处理线程的问题。
     *
     * @param text
     * @param durarion
     */
    private static void safeShow(final Context context, final String text, final int durarion) {
        sHandler = new Handler(Looper.getMainLooper());
        if (Looper.myLooper() != Looper.getMainLooper()) {//如果不是在主线程弹出吐司，那么抛到主线程弹
            sHandler.post(
                    new Runnable() {
                        @Override
                        public void run() {
                            showToast(context,text, durarion);
                        }
                    }
            );
        } else {
            showToast(context,text, durarion);
        }
    }

    /**
     * 弹出Toast，处理单例的问题。
     *
     * @param text
     * @param durarion
     */
    private static void showToast(Context context,String text, int durarion) {
        Toast  sToast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
        sToast.setDuration(durarion);
        sToast.setGravity(Gravity.CENTER,0,0);
        sToast.show();
        sHandler.removeCallbacksAndMessages(null);
    }

    public static void showGotCoinToast(Context context,String coinCount){
        Toast toast = new Toast(context);
        View view = LayoutInflater.from(context).inflate(R.layout.toast_get_coin,null);
        TextView textView = view.findViewById(R.id.tv_coin_count);
        textView.setText(coinCount);
        toast.setView(view);
        toast.setGravity(Gravity.CENTER,0,0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.show();
    }

    public static void showGotCoinToast(Context context,String hint,String coinCount){
        Toast toast = new Toast(context);
        View view = LayoutInflater.from(context).inflate(R.layout.toast_get_coin,null);
        TextView textView = view.findViewById(R.id.tv_coin_count);
        TextView hintView = view.findViewById(R.id.tv_success);
        hintView.setText(hint);
        textView.setText(coinCount);
        toast.setView(view);
        toast.setGravity(Gravity.CENTER,0,0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.show();
    }
}
